package test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaddleBall extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private double initialSpeedX = 5;
	private double initialSpeedY = -10;
	
	private double ballSpeedX;
	private double ballSpeedY;
	
	private double ballX;
	private double ballY;
	
	BufferedImage bf = null;
	
	public PaddleBall()
	{
		ballSpeedX	= initialSpeedX;
		ballSpeedY	= initialSpeedY;
		ballX				= 40;
		ballY				= 40;
		setSize(400,400);
		
		File imagefile = new File("pending.png");
		try {
			bf = ImageIO.read(imagefile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void start() throws InterruptedException
	{
		while(true)
		{
			// check if ball has hit the top wall
		    if (ballY <= 10) {
		        ballSpeedY = -ballSpeedY;
		    }
		    // check if the ball has hit the left wall
		    if (ballX <= 10) {
		        ballSpeedX = -ballSpeedX;
		    }
		    // check if the ball has hit the right wall
		    if (ballX >= 370) {
		        ballSpeedX = -ballSpeedX;
		    }
		    
		    // check if ball fell out the bottom
		    if (ballY >= 370) {
		    	ballSpeedY = -ballSpeedY;
		    }
		    
		    ballX += ballSpeedX;
			ballY += ballSpeedY;
			System.out.println("X="+ballX+", Y="+ballY);
		    repaint();
		    Thread.sleep(40);
		}
	}
	
	@Override
	public void paint(Graphics g) {
		Color background 	= new Color(0, 108, 135);
		g.setColor(background);
		g.fillRect(0, 0, 400, 400);
		//Color ballColor 		= new Color(255, 234, 0);
		//g.setColor(ballColor);
		//g.fillOval((int)ballX, (int)ballY, 20, 20);
		
		g.drawImage(bf,(int)ballX, (int)ballY, 20, 20, null, null);
	}
	
	public static void main(String[] args)
	{
		PaddleBall bb = new PaddleBall();
		JFrame jf = new JFrame();
		jf.setSize(400, 400);
		jf.add(bb);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		
		try {
			bb.start();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
